package com.huiyujiaoyu.powertime.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.base.BaseFragment;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleList2Bean;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.TabEntity;
import com.huiyujiaoyu.powertime.ui.adapter.AllFragmentPager2Adapter;
import com.huiyujiaoyu.powertime.ui.find.SearchResultFragment;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.KeyboardUtil;
import com.huiyujiaoyu.widget.ForbidenScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.attr.type;
import static com.huiyujiaoyu.base.AppContext.datas;


/**
 * Created by win7 on 2017/6/17.
 * 更多热门文章界面
 */

public class SearchResult1Activity extends BaseActivity {


    @BindView(R.id.edt_search)
    EditText mEdtSearch;
    @BindView(R.id.tv_cancle)
    TextView mTvCancle;
    @BindView(R.id.tab)
    CommonTabLayout mTab;
    @BindView(R.id.view_pager_2)
    ViewPager mViewPager2;
    private List<BaseFragment> mBaseFragments;
    private AllFragmentPager2Adapter adapter;
    private String[] titles;
    SearchResultFragment f1,f2;
    private String key;
    private List<IndexItemModel> datas;
    private SimpleList2Bean bean;

    public static void launch(Context context, String key) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.DATA, key);
        ActivityUtil.startActivity(context, SearchResult1Activity.class, bundle);
    }
    public void initView() {
        setTitleNoBar();
    }

    public void initData() {
        //历史数据
        mBaseFragments = new ArrayList<>();
        f1 = SearchResultFragment.newInstance(1);
        f2 = SearchResultFragment.newInstance(2);
        mBaseFragments.add(f1);
        mBaseFragments.add(f2);
        titles = new String[2];
        titles[0] = "内容";
        titles[1] = "专栏";
        adapter = new AllFragmentPager2Adapter(getSupportFragmentManager(),mBaseFragments);
        mViewPager2.setAdapter(adapter);
        ArrayList<CustomTabEntity> dts = new ArrayList<>();
        dts.add(new TabEntity(titles[0]));
        dts.add(new TabEntity(titles[1]));
        mTab.setTabData(dts);
        mTab.setCurrentTab(0);
        key = getIntent().getStringExtra(Constant.DATA);
        HttpMethods.getInstance().keywordss(mContext,getComp(),this,key,1);
        datas = new ArrayList<>();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_searchresult1;
    }


    @Override
    protected void initListener() {
        super.initListener();

        mEdtSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // 先隐藏键盘
                    KeyboardUtil.hideSoftKeyboard(SearchResult1Activity.this);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                     key = mEdtSearch.getText() + "";
                    if (!TextUtils.isEmpty(key)) {
                        HttpMethods.getInstance().keywordss(mContext,getComp(),SearchResult1Activity.this,key,1);
                        mEdtSearch.setText("");
                    }
                }
                return false;
            }
        });
        mTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mViewPager2.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mTab.setCurrentTab(position);
            }
        });
    }


    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_KEYWORDSS:
                datas.clear();
                bean = (SimpleList2Bean) o;
                 int size = bean.article.size();
                for (int i = 0; i <size ; i++) {
                    IndexItemModel indexItemModel = bean.article.get(i);
                    indexItemModel.header = "文章";
                    indexItemModel.isNotShow =  i>2;
                    indexItemModel.content_type = Constant.CONTENT_TYPE_WZ;
                    datas.add(indexItemModel);
                }
                size = bean.video.size();
                for (int i = 0; i <size ; i++) {
                    IndexItemModel indexItemModel = bean.video.get(i);
                    indexItemModel.header = "视频";
                    indexItemModel.isNotShow =  i>2;
                    indexItemModel.content_type =  Constant.CONTENT_TYPE_SP;
                    datas.add(indexItemModel);
                }
                size = bean.audio.size();
                for (int i = 0; i <size ; i++) {
                    IndexItemModel indexItemModel = bean.audio.get(i);
                    indexItemModel.header = "音频";
                    indexItemModel.content_type =  Constant.CONTENT_TYPE_YP;
                    indexItemModel.isNotShow =  i>2;
                    datas.add(indexItemModel);
                }
                f1.setData(datas);
                f2.setData(bean.column);
                break;
        }
    }

    public void setOpen(String header){
        switch (header) {
            case "文章":
                for (int i = 0; i < bean.article.size(); i++) {
                    bean.article.get(i).isNotShow = false;
                }
                break;
            case "音频":
                for (int i = 0; i < bean.audio.size(); i++) {
                    bean.audio.get(i).isNotShow = false;
                }
                break;
            case "视频":
                for (int i = 0; i < bean.video.size(); i++) {
                    bean.video.get(i).isNotShow = false;
                }
                break;
        }
    }


    @OnClick({R.id.tv_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancle:
                finish();
                break;
        }
    }

}
